package com.ddwl.user.service;

import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.common.util.MySqlUtil;
import com.ddwl.common.util.UUIDGenerator;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.user.AuditConfigBo;
import com.ddwl.schema.bo.user.AuditConfigPageBo;
import com.ddwl.schema.bo.user.AuditConfigUpdateBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.user.AuditConfigVo;
import com.ddwl.user.dao.AuditConfigDao;
import com.ddwl.user.model.AuditConfig;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author hemiaojun
 * @Date 2020/9/29 13:24
 */
@Service
public class AuditConfigService {

    @Resource
    private AuditConfigDao auditConfigDao;

    @Transactional(rollbackFor = Exception.class)
    public AuditConfigVo add(AuditConfigBo bo) {
        AuditConfig entity = BeanConverUtils.convertBean(bo, AuditConfig.class);
        entity.setId(UUIDGenerator.getShortUUID());
        auditConfigDao.insert(entity);
        return BeanConverUtils.convertBean(entity, AuditConfigVo.class);
    }

    public Integer checkStatus(AuditConfigBo bo) {
        AuditConfig entity = BeanConverUtils.convertBean(bo, AuditConfig.class);
        AuditConfig auditConfig=auditConfigDao.queryByOsAndVersionAndChannel(entity);
        return (auditConfig == null || auditConfig.getStatus() == null) ? 1 : auditConfig.getStatus();
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(AuditConfigUpdateBo bo) {
        AuditConfig entity = BeanConverUtils.convertBean(bo, AuditConfig.class);
        auditConfigDao.updateByPrimaryKeySelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        auditConfigDao.deleteById(id);
    }

    public PageVo<AuditConfigVo> page(PageBo<AuditConfigPageBo> pageBo) {
        PageHelper.startPage(pageBo.getPage(), pageBo.getPageSize(), MySqlUtil.orderBy(pageBo.getSort(), pageBo.isAsc()));
        PageInfo<AuditConfig> pageInfo = new PageInfo<>(auditConfigDao.getList(pageBo.getParam()));
        List<AuditConfigVo> list = BeanConverUtils.convertBeanList(pageInfo.getList(),AuditConfigVo.class);
        return new PageVo<>(pageInfo.getTotal(),pageBo.getPage(),pageBo.getPageSize(),list);
    }

    public void changeId() {
        List<AuditConfig> list=auditConfigDao.getAll();
        if (!CollectionUtils.isEmpty(list)) {
            Map<String, String> map = new HashMap<>();
            for (AuditConfig ac : list) {
                String id = ac.getId();
                if (id.length() == 11) {
                    long longId = UUIDGenerator.reverseShortUUID(id);
                    map.put(id, String.valueOf(longId));
                }
            }
            if (!CollectionUtils.isEmpty(map)) {
                auditConfigDao.changeId(map);
            }
        }
    }
}
